#include "xfile_general_operation.h"
#include "xrt_error.h"

namespace xge{

#ifdef XGE_USE_WIDE_CHAR
#define FIND_NEXT_FILE FindNextFileW
#define FIND_FIRST_FILE FindFirstFileW
#else
#define FIND_NEXT_FILE FindNextFileA
#define FIND_FIRST_FILE FindFirstFileA
#endif

  void FileSearch::Next() {
    if( FIND_NEXT_FILE(find_handle_,&find_data_) == FALSE ) {
      if( ::FindClose(find_handle_) == FALSE )
        WIN32_API_WARNING(_xT("FindClose"));
      find_handle_ = INVALID_HANDLE_VALUE;
    }
  }

  void FileSearch::Research( const char_t* wildcard ){
    if( find_handle_ != INVALID_HANDLE_VALUE ) 
      if( ::FindClose(find_handle_) == FALSE )
        WIN32_API_WARNING(_xT("FindClose"));
    if( ( find_handle_ = FIND_FIRST_FILE(wildcard,&find_data_)) == INVALID_HANDLE_VALUE ) {
      WIN32_API_WARNING(_xT("FindFirstFile"));
    } 
  }

  FileSearch::~FileSearch () {
    if( find_handle_ != INVALID_HANDLE_VALUE ) 
      if( ::FindClose(find_handle_) == FALSE )
        WIN32_API_WARNING(_xT("FindClose"));
  }

  bool CreateFolder( const char_t* path ) {
    BOOL ret=
#ifdef XGE_USE_WIDE_CHAR
    ::CreateDirectoryW(path,NULL);
#else
    ::CreateDirectoryA(path,NULL);
#endif
    if( ret == FALSE )
      WIN32_API_WARNING(_xT("CreateFolder"));
    return ret == TRUE;
  }


#undef FIND_NEXT_FILE
#undef FIND_FIRST_FILE


}// namespace xge